package com.marketingexpert.controller.personmgnt;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.marketingexpert.domain.User;
import com.marketingexpert.exception.MarketingExpertException;
import com.marketingexpert.service.storemgnt.StoreService;
import com.marketingexpert.service.storemgnt.StoreServiceFactory;
import com.marketingexpert.service.usermgnt.UserService;
import com.taobao.api.ApiException;
import com.taobao.api.domain.Shop;

public class GetStoreInfoController implements Controller {

	private String successView;
	
	private UserService userService;
	
	private StoreServiceFactory storeServiceFactory = null;
	
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		response.setContentType("application/x-json;charset=GBK"); 
		response.setHeader("Pragma", "No-cache"); 
		response.setHeader("Cache-Control", "no-cache"); 
		response.setHeader("Cache-Control", "no-store"); 
		response.setDateHeader("Expires", 0);  
		
		String userNick = (String)request.getSession().getAttribute("userNick");
		
		if(request.getSession().getAttribute("isAdmin") != null && ((Boolean)request.getSession().getAttribute("isAdmin")) == true) {
			return null;
		}
		StoreService taobaoService = getStoreServiceFactory().getService(StoreServiceFactory.TAOBAO);
		
		Shop shop = null;
		
		try {
			shop = taobaoService.getUserShop(userNick);
		} catch(MarketingExpertException e) {
			if(e.getErrCode().equals("")) {
				
			}
			e.printStackTrace();
			throw e;
		}
		
		Map<String,String> result = new HashMap<String,String>();
		
		PrintWriter out = response.getWriter(); 
		result.put("shopId", String.valueOf(shop.getSid()));
		result.put("shopTitle", shop.getTitle());
		
		JSONObject jsonList = JSONObject.fromObject(result); 
		
		out.print(jsonList);
		
		return null;
		
	}

	public String getSuccessView() {
		return successView;
	}

	public void setSuccessView(String successView) {
		this.successView = successView;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public StoreServiceFactory getStoreServiceFactory() {
		return storeServiceFactory;
	}

	public void setStoreServiceFactory(StoreServiceFactory storeServiceFactory) {
		this.storeServiceFactory = storeServiceFactory;
	}
	
	

}
